package com.lawliet.example.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lawliet.example.model.system.SysDict;
import com.lawliet.example.model.system.SysDictItem;
import com.lawliet.example.repository.system.SysDictItemMapper;
import com.lawliet.example.repository.system.SysDictMapper;
import com.lawliet.example.service.system.SysDictService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * Spring 启动后自动执行一次字典加载
     */
    @PostConstruct
    public void initDictCache() {
        loadDictToRedis();
        System.out.println("字典缓存已加载！");
    }

    /**
     * 获取字典翻译结果
     */
    @Override
    public void loadDictToRedis() {
        //获取所有的字典
        LambdaQueryWrapper<SysDict> dictCon = new LambdaQueryWrapper<>();
        dictCon.eq(SysDict::getStatus,1);//启用
        dictCon.eq(SysDict::getDelFlag,0);//未被删除
        List<SysDict> sysDicts = sysDictMapper.selectList(dictCon);
        for(SysDict sysDict : sysDicts){
            String dictCode = sysDict.getDictCode();//字典code
            LambdaQueryWrapper<SysDictItem> dictItemCon = new LambdaQueryWrapper<>();
            dictItemCon.eq(SysDictItem::getDictId,sysDict.getId());
            dictItemCon.orderByAsc(SysDictItem::getSort);
            List<SysDictItem> sysDictItems = sysDictItemMapper.selectList(dictItemCon);
            for(SysDictItem sysDictItem : sysDictItems){
                String key = sysDictItem.getItemValue();//某字典下的键
                String value = sysDictItem.getItemLabel();//某字典下的值
                //字典存入redis
                if(redisTemplate.hasKey("sys_dict:"+dictCode+":"+key)){
                    redisTemplate.delete("sys_dict:"+dictCode+":"+key);
                }
                redisTemplate.opsForValue().set("sys_dict:"+dictCode+":"+key,value);
            }
        }

    }

    /**
     * 数据字典删除
     * @param ids 待删除的id集合
     */
    @Override
    @Transactional
    public void deleteByIds(String ids) {
        //删除字典
        List<String> idList = Arrays.asList(ids.split(","));
        this.removeByIds(idList);
        for(String id : idList){
            Long dictId = Long.parseLong(id);
            //删除对应的字典项
            LambdaQueryWrapper<SysDictItem> con = new LambdaQueryWrapper<>();
            con.eq(SysDictItem::getDictId,dictId);
            sysDictItemMapper.delete(con);
        }
    }
}
